/*
 * NodeFactory.cpp
 *
 *  Created on: 2010-6-6
 *      Author: guobc
 */

#include "NodeFactory.h"
using namespace HA;

NodeFactory::NodeFactory() :
	elements() {
	registerElements();
}

NodeFactory::~NodeFactory() {
	elements.clear();
}

void NodeFactory::registerElement(Element* element) {
	string* ps = element->getIds();
	string id;
	while (!ps->empty()) {
		id = *ps++;
		elements[id] = element;
	}
}

void NodeFactory::registerElements() {
	registerElement(new AncherElement());
	registerElement(new AppletElement());
	registerElement(new BaseElement());
	registerElement(new BodyElement());
	registerElement(new DefinitionList());
	registerElement(new DefinitionListItem());
	registerElement(new DivElement());
	registerElement(new DoctypeElement());
	registerElement(new FormElement());
	registerElement(new FrameElement());
	registerElement(new FramesetElement());
	registerElement(new HeadElement());
	registerElement(new HeadingElement());
	registerElement(new HTMLElement());
	registerElement(new ImageElement());
	registerElement(new InputElement());
	registerElement(new JspElement());
	registerElement(new LabelElement());
	registerElement(new ListElement());
	registerElement(new ListItemElement());
	registerElement(new MetaElement());
	registerElement(new ObjectElement());
	registerElement(new OptionElement());
	registerElement(new ParagraphElement());
	registerElement(new ParamElement());
	registerElement(new ScriptElement());
	registerElement(new SelectElement());
	registerElement(new SpanElement());
	registerElement(new StyleElement());
	registerElement(new TableColumnElement());
	registerElement(new TableElement());
	registerElement(new TableHeadElement());
	registerElement(new TableRowElement());
	registerElement(new TextareaElement());
	registerElement(new TitleElement());
}

Text* NodeFactory::createStringNode(const string& text, int start, int end) {
	TextNode* node = new TextNode();
//	node->setDocument(document);
	node->setStartLine(start);
	node->setEndLine(end);
	return node;

}

Comment* NodeFactory::createCommentNode(const string& text,  int start, int end) {
	CommentNode* node = new CommentNode();
//	node->setDocument(document);
	node->setStartLine(start);
	node->setEndLine(end);
	return node;

}

Element* NodeFactory::createElementNode(const string& Id,  int start, int end) {
	Element* element = NULL;
	if (elements.count(Id) == 1) {
		ASTNode* node = elements[Id]->newInstance();
		element = dynamic_cast<Element*> (node);
	} else {
		element = new CommonElement();
	}

	if (element != NULL) {
//		element->setDocument(document);
		element->setStartLine(start);
		element->setEndLine(end);
	}

	return element;
}
